package com.power.ding.dao.impl;

import com.plf.common.utils.StringUtil;
import com.power.ding.exception.DingException;
import com.power.ding.model.pageInfo.Di_todoPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.power.ding.dao.Di_todoDaoCustom;

import java.util.List;
import java.util.Map;

/**
 * 钉钉待办发送 持久层实现(hibernate)
 */

@Repository
public class Di_todoDaoImpl implements Di_todoDaoCustom {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> getTodoListByUserid(Di_todoPageInfo pageInfo) throws DingException {
        StringBuffer sql = new StringBuffer(
                "SELECT d.id, d.senduserid, d.sendusername, d.senddeptid, d.senddeptname, d.receiveuserid, d.receiveusername, d.receivedeptid, d.receivedeptname, d.applyid, to_char(d.sendtime,'yyyy-mm-dd') sendtime, d.pstatus, to_char(d.ptime,'yyyy-mm-dd') ptime, a.applyname, b.acceptcode FROM di_todo d LEFT JOIN apl_admreview a on d.applyid = a.id LEFT JOIN apl_main b on d.applyid = b.id where 1 = 1"
        );
        if (!StringUtil.isEmpty(pageInfo.getF_receiveUserId())) {
            sql.append(" AND d.receiveuserid = '" + pageInfo.getF_receiveUserId() + "'");
        }
        if (!StringUtil.isEmpty(pageInfo.getF_pStatus())) {
            sql.append(" AND d.pstatus = " + pageInfo.getF_pStatus());
        }
        sql.append(" order by sendtime asc");
        Map<String, Object> mp = jdbcTemplate.queryForMap("select count(1) count from (" + sql + ") x");
        Integer count = ((Number) mp.get("count")).intValue();
        pageInfo.setAllcount(count);
        sql.append(" LIMIT " + pageInfo.getPageSize() * (pageInfo.getPageNumber() - 1) + "," + pageInfo.getPageSize());
        return jdbcTemplate.queryForList(sql.toString());
    }

    @Override
    public Map<String, Object> getTodo(String id) throws DingException {
        StringBuffer sql = new StringBuffer("SELECT d.id, d.senduserid, d.sendusername, d.senddeptid, d.senddeptname, d.receiveuserid, d.receiveusername, d.receivedeptid, d.receivedeptname, d.applyid, to_char(d.sendtime,'yyyy-mm-dd') sendtime, d.pstatus, to_char(d.ptime,'yyyy-mm-dd') ptime, d.mattype, a.applyname, b.acceptcode FROM di_todo d LEFT JOIN apl_admreview a on d.applyid = a.id LEFT JOIN apl_main b on d.applyid = b.id where 1 = 1");
        sql.append(" and d.id='" + id + "'");
        return jdbcTemplate.queryForMap(sql.toString());
    }

    @Override
    public Map<String, Object> getAttachByApplyid(String applyid, Integer mattype) {
        List<Map<String, Object>> attachList = null;
        Map<String, Object> attach = null;
        StringBuffer sql = new StringBuffer("SELECT * FROM aprv_process_materials WHERE applyid = '");
        sql.append(applyid + "' AND mattype = " + mattype + " AND sign = 6011302");
        sql.append(" ORDER BY createtime desc");
        attachList = jdbcTemplate.queryForList(sql.toString());
        if (attachList.size() > 0) {
            attach = attachList.get(0);
            return attach;
        } else {
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> getFileListByApplyId(String applyid, Integer mattype) {
        StringBuffer sql = new StringBuffer("SELECT * FROM aprv_process_materials WHERE applyid = '");
        sql.append(applyid + "' AND mattype = " + mattype);
        return jdbcTemplate.queryForList(sql.toString());
    }
}